﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class team_buy : FBasePage
{
    // Fields

    protected int Farefree = 0;
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public List<Order> orderlist = new List<Order>();

    public string strCurrency = "";
    public string strDelivery = "";
    public string strExpress = "";
    public string strFarefree = "";
    public string strFee = "";
    public string strMoney = "";
    public string strOrigin = "";
    public string strPer_number = "";
    public string strPrice = "";
    public string strQuantity = "";
    public string strTitle = "";
    public string strToTal = "";
    protected NameValueCollection teamarr = new NameValueCollection();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Team teammodel = new Team();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public UserInfo usermodel = new UserInfo();

    // Methods
    private string GetCurrency()
    {
        string str = "";
        DataSet list = new Maticsoft.BLL.System().GetList("");
        if (list != null)
        {
            if (list.Tables.Count <= 0)
            {
                return str;
            }
            if (list.Tables[0].Rows.Count > 0)
            {
                str = list.Tables[0].Rows[0]["Currency"].ToString();
            }
        }
        return str;
    }

    private void initPage()
    {
        Maticsoft.BLL.Team team;
        string s = "";
        string str2 = "0";
        if ((base.Request.Form["quantity"] == null) || (base.Request.Form["quantity"].ToString() == ""))
        {
            str2 = "1";
        }
        else
        {
            str2 = base.Request.Form["quantity"].ToString();
        }
        StringBuilder builder = new StringBuilder();
        if ((base.Request["id"] == null) || (base.Request["id"].ToString() == ""))
        {
            if ((base.Request["orderid"] != null) && (base.Request["orderid"].ToString() != ""))
            {
                if (!Utility.IsNum(base.Request["orderid"].ToString()))
                {
                    this.SetError("参数错误！");
                    base.Response.Redirect("/index.aspx");
                }
                else
                {
                    Order order2 = new Maticsoft.BLL.Order().GetModel(int.Parse(base.Request["orderid"].ToString()));
                    if (order2 != null)
                    {
                        str2 = order2.Quantity.ToString();
                        s = order2.Team_id.ToString();
                        this.txtremark.Value = order2.Remark;
                        this.settingsmobile.Value = order2.Mobile;
                        this.settingsaddress.Value = order2.Address;
                        this.settingsrealname.Value = order2.Realname;
                        this.settingszipcode.Value = order2.Zipcode;
                        goto Label_0265;
                    }
                    this.SetError("没有该订单！");
                    base.Response.Redirect("/index.aspx");
                }
            }
            else
            {
                base.Response.Redirect("/index.aspx");
                base.Response.End();
            }
            return;
        }
        s = base.Request["id"].ToString();
    Label_0265:
        team = new Maticsoft.BLL.Team();
        Team model = team.GetModel(int.Parse(s));
        if (model == null)
        {
            this.SetError("没有该项目信息");
            base.Response.Redirect("/index.aspx");
            base.Response.End();
        }
        else
        {
            TeamState state = base.GetState(model);
            if ((state != TeamState.begin) && (state != TeamState.successbuy))
            {
                this.SetError("该项目不能购买");
                base.Response.Redirect("/index.aspx");
                base.Response.End();
            }
            else
            {
                float num4;
                this.strFarefree = model.Farefree.ToString();
                this.Farefree = model.Farefree;
                this.strDelivery = model.Delivery;
                this.strExpress = model.Express;
                this.strPrice = model.Team_price.ToString();
                if (this.strDelivery == "express")
                {
                    this.pExpress.Visible = true;
                }
                if (int.Parse(str2) < int.Parse(this.strFarefree))
                {
                    this.strFee = model.Fare.ToString();
                }
                else if (str2 == "1")
                {
                    this.strFee = model.Fare.ToString();
                }
                else
                {
                    this.strFee = "0";
                }
                this.strPer_number = model.Per_number.ToString();
                this.strCurrency = this.GetCurrency();
                float num = float.Parse(str2) * float.Parse(model.Team_price.ToString());
                if (int.Parse(str2) < int.Parse(this.strFarefree))
                {
                    num4 = num + float.Parse(this.strFee);
                    this.strOrigin = num4.ToString();
                }
                else if (str2 == "1")
                {
                    num4 = num + float.Parse(this.strFee);
                    this.strOrigin = num4.ToString();
                }
                else
                {
                    this.strOrigin = num.ToString();
                }
                this.strToTal = num.ToString();
                this.strPrice = model.Team_price.ToString();
                this.teamarr = Helper.GetObjectProtery(model);
                builder.Append("<tr>");
                builder.Append(" <td class=\"deal-buy-desc\">" + model.Title + "</td>");
                builder.Append("<td class=\"deal-buy-quantity\">");
                string str3 = (model.Per_number == 1) ? "readonly" : "";
                builder.Append(string.Concat(new object[] { " <input type=\"text\" runat=\"server\" id='deal-buy-quantity-input'   class=\"input-text f-input\" maxlength=\"4\" name=\"quantity\" value=\"", str2, "\" ff=\"", model.Farefree, "\" ", str3, " />" }));
                builder.Append("<br /><span style=\"font-size:12px;color:gray;\">");
                if (model.Per_number == 0)
                {
                    builder.Append("最多9999件");
                }
                else
                {
                    builder.Append("最多" + model.Per_number.ToString() + "件<input id=\"num\" type=\"hidden\" value='" + model.Per_number.ToString() + "' />");
                }
                builder.Append("</span></td>");
                builder.Append("<td class=\"deal-buy-multi\">x</td>");
                builder.Append("<td class=\"deal-buy-price\"><span class=\"money\">" + this.GetCurrency() + "</span><span id=\"deal-buy-price\">" + model.Team_price.ToString() + "</span></td>");
                builder.Append("<td class=\"deal-buy-equal\">=</td>");
                builder.Append("<td class=\"deal-buy-total\"><span class=\"money\">" + this.GetCurrency() + "</span><span id=\"deal-buy-total\">" + num.ToString() + "</span></td>");
                builder.Append("</tr> ");
                this.ltPro.Text = builder.ToString();
                string str4 = "0";
                StringBuilder builder2 = new StringBuilder();
                if (int.Parse(str2) < int.Parse(this.strFarefree))
                {
                    str4 = model.Fare.ToString();
                }
                else if ((str2 == "1") && (this.strFarefree != "1"))
                {
                    str4 = model.Fare.ToString();
                }
                else
                {
                    str4 = "0";
                }
                if (model.Delivery == "express")
                {
                    builder2.Append("<tr>");
                    builder2.Append("<td class=\"deal-buy-desc\">快递</td>");
                    builder2.Append("<td class=\"deal-buy-quantity\"></td>");
                    builder2.Append("<td class=\"deal-buy-multi\"></td>");
                    builder2.Append("<td class=\"deal-buy-price\"><span class=\"money\">" + this.GetCurrency() + "</span><span id=\"deal-express-price\" v=\"" + this.strFee + "\">" + str4 + "</span></td>");
                    builder2.Append("<td class=\"deal-buy-equal\">=</td>");
                    builder2.Append("<td class=\"deal-buy-total\"><span class=\"money\">" + this.GetCurrency() + "</span><span id=\"deal-express-total\" v=\"" + this.strFee + "\">" + str4 + "</span></td>");
                    builder2.Append("</tr>");
                }
                this.ltExpress.Text = builder2.ToString();
                StringBuilder builder3 = new StringBuilder();
                builder3.Append("<tr class=\"order-total\">");
                builder3.Append("<td class=\"deal-buy-desc\"><strong>订单总额：</strong></td>");
                builder3.Append("<td class=\"deal-buy-quantity\"></td>");
                builder3.Append("<td class=\"deal-buy-multi\"></td>");
                builder3.Append("<td class=\"deal-buy-price\"></td>");
                builder3.Append("<td class=\"deal-buy-equal\">=</td>");
                if (model.Delivery == "express")
                {
                    string[] strArray = new string[] { "<td class=\"deal-buy-total\"><span class=\"money\">", this.GetCurrency(), "</span><strong id=\"deal-buy-total-t\">", (num + float.Parse(str4)).ToString(), "</strong></td>" };
                    builder3.Append(string.Concat(strArray));
                }
                else
                {
                    builder3.Append("<td class=\"deal-buy-total\"><span class=\"money\">" + this.GetCurrency() + "</span><strong id=\"deal-buy-total-t\">" + num.ToString() + "</strong></td>");
                }
                builder3.Append("</tr>");
                this.ltTotal.Text = builder3.ToString();
            }
        }
    }

    private void initPostInfo()
    {
        if (!this.Page.User.Identity.IsAuthenticated)
        {
            base.Response.Redirect("~/account/Login.aspx");
            base.Response.End();
        }
        else
        {
            string name = this.Page.User.Identity.Name;
            Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
            string strWhere = "";
            strWhere = string.Format(" Username='{0}'", name);
            DataSet list = info.GetList(strWhere);
            if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
            {
                this.settingszipcode.Value = list.Tables[0].Rows[0]["Zipcode"].ToString();
                this.settingsrealname.Value = list.Tables[0].Rows[0]["Realname"].ToString();
                this.settingsmobile.Value = list.Tables[0].Rows[0]["Mobile"].ToString();
                this.settingsaddress.Value = list.Tables[0].Rows[0]["Address"].ToString();
                this.strMoney = list.Tables[0].Rows[0]["Money"].ToString();
            }
        }
    }

    public void Judge(int teamid)
    {
        this.orderlist = this.orderbll.GetByUserid(this.userbll.Getuserid(base.UserName), teamid);
        this.teammodel = this.teambll.GetModel(teamid);
        if (this.teammodel == null)
        {
            this.SetError("没有该项目");
            base.Response.Redirect("/index.aspx");
            base.Response.End();
        }
        else
        {
            int num = 0;
            bool flag = false;
            int id = 0;
            for (int i = 0; i < this.orderlist.Count; i++)
            {
                if (this.orderlist[i].State == "unpay")
                {
                    flag = true;
                    id = this.orderlist[i].Id;
                    break;
                }
                num++;
            }
            if (flag)
            {
                base.Response.Redirect("~/order/check.aspx?orderid=" + id);
                base.Response.End();
            }
            else
            {
                TeamState state = base.GetState(this.teammodel);
                if ((state != TeamState.begin) && (state != TeamState.successbuy))
                {
                    this.SetError("该项目不能购买");
                    base.Response.Redirect("/index.aspx");
                    base.Response.End();
                }
                else if ((this.teammodel.Buyonce == "Y") && (num > 0))
                {
                    this.SetError("您已经购买过此项目，当前项目只允许购买一次");
                    base.Response.Redirect("~/team.aspx?id=" + teamid);
                    base.Response.End();
                }
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
            {
                base.Response.Redirect("~/account/Login.aspx");
                base.Response.End();
            }
            else
            {
                if (base.Request["id"] != null)
                {
                    if (!Utility.IsNum(base.Request["id"].ToString()))
                    {
                        this.SetError("参数错误！");
                        base.Response.Redirect("/index.aspx");
                        return;
                    }
                    this.Judge(Convert.ToInt32(base.Request["id"].ToString()));
                }
                this.initPostInfo();
                this.initPage();
            }
        }
    }

    protected void submit_ServerClick(object sender, EventArgs e)
    {
        Maticsoft.BLL.Order order;
       Order order2;
        string s = "";
        if ((base.Request["orderid"] != null) && (base.Request["orderid"].ToString() != ""))
        {
            if (!Utility.IsNum(base.Request["orderid"].ToString()))
            {
                this.SetError("参数错误！");
                base.Response.Redirect("/index.aspx");
                return;
            }
            string str2 = base.Request["orderid"].ToString();
            order = new Maticsoft.BLL.Order();
            order2 = order.GetModel(int.Parse(str2));
            if (order2 != null)
            {
                s = order2.Team_id.ToString();
            }
        }
        else
        {
            if (!Utility.IsNum(base.Request["id"].ToString()))
            {
                this.SetError("参数错误！");
                base.Response.Redirect("/index.aspx");
                return;
            }
            s = base.Request["id"].ToString();
            this.Judge(Convert.ToInt32(base.Request["id"].ToString()));
        }
        Maticsoft.Model.Team model = new Maticsoft.BLL.Team().GetModel(int.Parse(s));
        if (model == null)
        {
            this.SetError("没有该项目信息！");
        }
        else
        {
            this.strFarefree = model.Farefree.ToString();
            this.strDelivery = model.Delivery;
            this.strExpress = model.Express;
            this.strPrice = model.Team_price.ToString();
            this.strFee = model.Fare.ToString();
            float num = int.Parse(base.Request.Form["quantity"].ToString()) * float.Parse(model.Team_price.ToString());
            if (model.Delivery == "express")
            {
                float num6;
                if (int.Parse(base.Request.Form["quantity"].ToString()) < int.Parse(this.strFarefree))
                {
                    num6 = num + model.Fare;
                    this.strOrigin = num6.ToString();
                }
                else if ((base.Request["quantity"] == "1") && (this.strFarefree != "1"))
                {
                    num6 = num + model.Fare;
                    this.strOrigin = num6.ToString();
                }
                else if (this.strFarefree == "0")
                {
                    this.strOrigin = (num + model.Fare).ToString();
                }
                else
                {
                    this.strOrigin = num.ToString();
                }
            }
            else
            {
                this.strFee = "0";
                this.strOrigin = num.ToString();
            }
            int orderid = 0;
            if (Convert.ToInt32(base.Request["quantity"]) < 0)
            {
                this.SetError("友情提示：请输入正确的数字");
            }
            else
            {
                if ((base.Request["orderid"] != null) && (base.Request["orderid"].ToString() != ""))
                {
                    if (!Utility.IsNum(base.Request["orderid"].ToString()))
                    {
                        this.SetError("参数错误！");
                        base.Response.Redirect("/index.aspx");
                        return;
                    }
                    orderid = int.Parse(base.Request["orderid"].ToString());
                    this.UpdateOrder(orderid, this.strOrigin);
                }
                else
                {
                    order = new Maticsoft.BLL.Order();
                    order2 = new Order();
                    order2.Address = this.settingsaddress.Value;
                    order2.Quantity = int.Parse(base.Request.Form["quantity"].ToString());
                    order2.Create_time = DateTime.Now;
                    order2.Zipcode = this.settingszipcode.Value;
                    if (this.strDelivery == "express")
                    {
                        order2.Express = "Y";
                    }
                    else
                    {
                        order2.Express = "N";
                    }
                    order2.Price = decimal.Parse(this.strPrice);
                    order2.Fare = decimal.Parse(this.strFee);
                    order2.Mobile = this.settingsmobile.Value;
                    order2.Origin = decimal.Parse(this.strOrigin);
                    order2.Realname = this.settingsrealname.Value;
                    string str3 = this.txtremark.Value;
                    if (str3.IndexOf(@"\") > 0)
                    {
                        str3 = str3.Replace(@"\", @"\\");
                    }
                    order2.Remark = str3;
                    order2.State = "unpay";
                    order2.Team_id = int.Parse(s);
                    string name = this.Page.User.Identity.Name;
                    Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
                    string strWhere = "";
                    strWhere = string.Format(" Username='{0}'", name);
                    DataSet list = info.GetList(strWhere);
                    if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
                    {
                        order2.User_id = int.Parse(list.Tables[0].Rows[0]["id"].ToString());
                    }
                    int num3 = 0;
                    if (model.Per_number == 0)
                    {
                        num3 = 0x270f;
                    }
                    else
                    {
                        num3 = int.Parse(model.Per_number.ToString());
                    }
                    if (num3 < order2.Quantity)
                    {
                        this.SetError("每人限购数量" + num3.ToString());
                        base.Response.Redirect("buy.aspx?id=" + s);
                        base.Response.End();
                        return;
                    }
                    if (base.CurrentCity != null)
                    {
                        order2.City_id = base.CurrentCity.Id;
                    }
                    order2.Ip_Address = CookieHelper.GetCookieValue("gourl");
                    order.Add(order2);
                    orderid = order.GetMaxId();
                }
                base.Response.Redirect("~/order/check.aspx?orderid=" + orderid);
                base.Response.End();
            }
        }
    }

    private void UpdateOrder(int orderid, string strOrigin)
    {
        Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
        int quantity = int.Parse(base.Request.Form["quantity"].ToString());
        string remark = this.txtremark.Value;
        string realname = this.settingsrealname.Value;
        string mobile = this.settingsmobile.Value;
        string address = this.settingsaddress.Value;
        string zipcode = this.settingszipcode.Value;
        order.UpdateOrder(orderid, quantity, realname, mobile, zipcode, address, remark, strOrigin);
    }

}